package com.aaa.controller;

import com.aaa.dao.UserinfoDao;
import com.aaa.dao.impl.UserinfoDaoImpl;
import com.aaa.entity.MyReturn;
import com.aaa.utils.MyTools;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "UsersServlet",urlPatterns = "/userinfo")
public class UsersServlet extends HttpServlet {
    UserinfoDao userinfoDao = new UserinfoDaoImpl();
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //将所有的参数存入一个map集合中
            Map<String, Object> map = new HashMap();
            //获取所有的参数名集合
            Enumeration<String> parameterNames = request.getParameterNames();
            //遍历所有的参数名
            while (parameterNames.hasMoreElements()) {
                /* System.out.println(parameterNames.nextElement());*/
                //获取第一个参数名
                String key = parameterNames.nextElement();
                //根据参数名获取参数值
                Object value = request.getParameter(key);
                //将参数名和参数值存入map集合中
                map.put(key, value);
            }
            MyReturn myReturn = new MyReturn();
            switch (map.get("method") + "") {
                case "select":
                    List<Map<String, Object>> userInfoList = userinfoDao.findAllUserInfoPage(map);

                    myReturn.setCode(0);
                    //获取数据库的总条数
                    myReturn.setCount(userinfoDao.getCount(map));
                    myReturn.setMsg("");
                    myReturn.setData(userInfoList);

                    break;
                case "save":
                    //需要额外处理从页面出入的参数
                    String password=map.get("password").toString();
                    Map<String,String> passwordSalt=MyTools.makePasswordSalt(password);
                    map.put("password",passwordSalt.get("password"));
                    map.put("salt",passwordSalt.get("salt"));
                    //保存数据到数据库,insert into userinfo ( username, password, salt, deptid, avatar) values (?,?,?,?,?)
                    boolean saveUserInfo = userinfoDao.saveUserInfo(map);
                    if (saveUserInfo) {
                        MyTools.getSuccessResult(myReturn);
                    } else {
                        MyTools.getFailResult(myReturn);
                    }

                    break;
                case "delete":
                    //删除操作
                    boolean deleteUserInfoById = userinfoDao.deleteUserInfoById(map);
                    if (deleteUserInfoById) {
                        MyTools.getSuccessResult(myReturn);
                    } else {
                        MyTools.getFailResult(myReturn);
                    }
                    break;
                default:
                    System.out.println("不支持的方法");
                    break;
            }
            //将返回的数据对象进行json字符串转换
            String jsonString = JSON.toJSONString(myReturn);
            response.getWriter().write(jsonString);


        }

}
